home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / e / e_clock.lha / clock.e < prev   
Text File  |  1995-11-05  |  11KB  |  283 lines

  1. /*
  2.   clock.e
  3.  
  4.   Author: Horst Schumann
  5.           Helmstedter Str. 18
  6.           39167 Irxleben
  7.           Germany
  8.  
  9.           e-mail: hschuman@cs.uni-magdeburg.de (until June, 1996)
  10.  
  11.  
  12.   A little clock program written with Amiga E
  13.   -------------------------------------------
  14.  
  15.   Thanks to Wouter van Oortmerssen for the programming environment
  16.   of Amiga E (and for the release of version 3.2a with which my
  17.   code for the timer.device finally worked).
  18.  
  19.   This is just an example for an analogue clock. I tried to do it
  20.   as system friendly as possible, but a few calculations are still
  21.   in there that take some time, so the system is getting slower,
  22.   if the program is running more than 10 times simultaneously.
  23.   That might be due to some trigonometric calculations. (I did not
  24.   want to put in a look-up table to keep the program small.)
  25.  
  26.   As stated before, this is a simple clock program. I wrote it in
  27.   E just to try the language and to have a clock I can customize
  28.   to my personal preferences. These personal things are not in
  29.   this release, it is just a simple clock written in E for
  30.   the E-community to work with.
  31.  
  32.   Copyright: Use it as best as you can. It is public domain.
  33.  
  34.   "Bug": - might be possible to optimize further
  35.  
  36. */
  37.  
  38. OPT OSVERSION=37
  39.  
  40. MODULE 'intuition/screens',
  41.        'devices/timer',
  42.        'intuition/intuition',
  43.        'exec/ports',
  44.        'exec/io'
  45.  
  46. DEF  pi_div_6=0.52359,            -> value for PI/6 (for speed)
  47.      pi_div_30=0.10471,           -> value for PI/30 (for speed)
  48.      hl=0.5,                      -> \
  49.      ml=0.8,                      ->  > relative length of hands
  50.      sl=0.9,                      -> /
  51.      hlenx,hleny,                 -> \
  52.      mlenx,mleny,                 ->  > absolute length of hands
  53.      slenx,sleny,                 -> /
  54.      win:PTR TO window,           -> pointer to window structure
  55.      hour,minute,second,micro,    -> time values
  56.      oldhour=0,                   -> \
  57.      oldminute=0,                 ->  > time of last get_time
  58.      oldsecond=0,                 -> /
  59.      midx,midy,                   -> center of window x and y
  60.      radx,rady,                   -> length from center to border
  61.      hrad,mrad,srad               -> radians for hour, minute and second
  62.  
  63. PROC main()
  64. DEF screen:PTR TO screen,         -> pointer to screen structure
  65.     tr:PTR TO timerequest,        -> timerequest structure
  66.     imess:PTR TO intuimessage,    -> intuition message dtructure
  67.     msg:PTR TO mp,                -> pointer to message port
  68.     quit=FALSE,                   -> flag for main loop
  69.     sig,intui_sig,timer_sig,      -> space for signal bits
  70.     class                         -> message class
  71.  
  72.   IF msg:=CreateMsgPort()    -> create port for timer
  73.     IF tr:=CreateIORequest(msg,SIZEOF timerequest)
  74.       IF OpenDevice('timer.device',UNIT_MICROHZ,tr,0)=0
  75.         IF screen:=(LockPubScreen(NIL))
  76.           IF win:=OpenWindowTagList(NIL,
  77.                                    [WA_TOP,         screen.height/2,
  78.                                     WA_LEFT,        screen.width/2,
  79.                                     WA_WIDTH,       screen.width/6,
  80.                                     WA_HEIGHT,      screen.height/4,
  81.                                     WA_CLOSEGADGET, TRUE,
  82.                                     WA_ACTIVATE,    TRUE,
  83.                                     WA_DRAGBAR,     TRUE,
  84.                                     WA_DEPTHGADGET, TRUE,
  85.                                     WA_SIZEBBOTTOM, TRUE,
  86.                                     WA_SIZEGADGET,  TRUE,
  87.                                     WA_MINHEIGHT,   50,
  88.                                     WA_MINWIDTH,    65,
  89.                                     WA_MAXHEIGHT,   -1,
  90.                                     WA_MAXWIDTH,    -1,
  91.                                     WA_IDCMP,       IDCMP_CLOSEWINDOW OR
  92.                                                     IDCMP_CHANGEWINDOW,
  93.                                     WA_TITLE,       'Simple E Clock',
  94.                                     0,0])
  95.             SetStdRast(win.rport)
  96.             /*
  97.               calculate the center of the window and the distance
  98.               from there to the borders
  99.             */
  100.             intui_sig:=Shl(1,win.userport.sigbit)
  101.             timer_sig:=Shl(1,msg.sigbit)
  102.             radx:=win.width-win.borderleft-win.borderright/2-1
  103.             midx:=radx+win.borderleft
  104.             rady:=win.height-win.bordertop-win.borderbottom/2-1
  105.             midy:=rady+win.bordertop       -> set center and radius
  106.             hlenx:=(radx)!*hl!             ->
  107.             mlenx:=(radx)!*ml!              ->
  108.             slenx:=(radx)!*sl!               -> set length of hands
  109.             hleny:=(rady)!*hl!               ->
  110.             mleny:=(rady)!*ml!              ->
  111.             sleny:=(rady)!*sl!             ->
  112.             CurrentTime({second},{micro})                ->
  113.             second:=second-Mul(Div(second,86400),86400) ->
  114.             hour:=Div(second,3600)                      ->
  115.             oldhour:=hour                              ->
  116.             second:=second-Mul(hour,3600)             -> init time values
  117.             minute:=Div(second,60)                     ->
  118.             oldminute:=minute                           ->
  119.             second:=second-Mul(minute,60)                ->
  120.             oldsecond:=second                            ->
  121.             dialplate()
  122.             tr.io.command:=TR_ADDREQUEST     ->
  123.             tr.time.secs:=0                   -> set timer request
  124.             tr.time.micro:=1000000-micro     ->
  125.             SendIO(tr)                       -> send the request
  126.             sig:=0                           -> reset signal bits
  127.             WHILE quit=FALSE
  128.               class:=NIL
  129.               IF sig AND intui_sig                -> message from Intuition
  130.                 WHILE imess:=GetMsg(win.userport) -> get message(s)
  131.                   class:=imess.class
  132.                   IF class=IDCMP_CHANGEWINDOW     -> user changed window
  133.                     RefreshWindowFrame(win)       -> redraw window frame
  134.                     radx:=win.width-win.borderleft-win.borderright/2-1
  135.                     midx:=radx+win.borderleft
  136.                     rady:=win.height-win.bordertop-win.borderbottom/2-1
  137.                     midy:=rady+win.bordertop      -> update center and radius
  138.                     hlenx:=(radx)!*hl!        ->
  139.                     mlenx:=(radx)!*ml!         ->
  140.                     slenx:=(radx)!*sl!          -> change length of hands
  141.                     hleny:=(rady)!*hl!          ->
  142.                     mleny:=(rady)!*ml!         ->
  143.                     sleny:=(rady)!*sl!        ->
  144.                     dialplate()            -> clear window and draw clock
  145.                   ELSEIF class=IDCMP_CLOSEWINDOW  -> user clicked close gadget
  146.                     quit:=TRUE
  147.                   ENDIF
  148.                   ReplyMsg(imess)
  149.                 ENDWHILE
  150.               ENDIF  -> no 'ELSEIF', because both events might have occured
  151.               IF sig AND timer_sig               -> timer event
  152.                 WHILE GetMsg(msg)=tr
  153.                   get_time()
  154.                   tr.time.secs:=0
  155.                   tr.time.micro:=1000000-micro   -> wait for next second
  156.                   SendIO(tr)                     -> send timer request
  157.                   clock()                        -> update clock
  158.                 ENDWHILE
  159.               ENDIF
  160.                 IF quit                  -> in case, user wants to quit
  161.                   AbortIO(tr)            -> end the last timer request
  162.                   WaitIO(tr)             -> wait for it to end
  163.                 ELSE
  164.                   sig:=Wait(timer_sig OR intui_sig)  -> wait for next event
  165.                 ENDIF
  166.             ENDWHILE   -> quit=FALSE
  167.             CloseWindow(win)
  168.           ELSE
  169.             WriteF('Could not open window!')
  170.           ENDIF -> if OpenWindow
  171.           UnlockPubScreen(NIL,screen)
  172.         ELSE
  173.           WriteF('Could not lock public screen!')
  174.         ENDIF   -> if LockPubScreen
  175.         CloseDevice(tr)
  176.       ELSE
  177.         WriteF('Could not open timer.device!')
  178.       ENDIF -> if OpenDevice
  179.       DeleteIORequest(tr)
  180.     ELSE
  181.       WriteF('Could not create IO request!')
  182.     ENDIF -> if CreateIORequest
  183.     DeleteMsgPort(msg)
  184.   ELSE
  185.     WriteF('Could not create message port!')
  186.   ENDIF     -> if CreateMsgPort
  187. ENDPROC
  188.  
  189.  
  190. PROC get_time()            -> puts the current time into global variables
  191.                            -> hour, minute, second and updates
  192.                            -> oldhour, oldminute, oldsecond
  193.  
  194. DEF curtime                -> space for current time
  195.  
  196.   CurrentTime({curtime},{micro})
  197.   oldsecond:=second                             -> save last value
  198.   second:=curtime-Mul(Div(curtime,86400),86400) -> take days out of number
  199.   oldhour:=hour                                 -> save last value
  200.   hour:=Div(second,3600)                        -> calculate hours of day
  201.   second:=second-Mul(hour,3600)                 -> take hours out
  202.   oldminute:=minute                             -> save last value
  203.   minute:=Div(second,60)                        -> get the mins of the hour
  204.   second:=second-Mul(minute,60)                 -> take mins out leave secs
  205. ENDPROC
  206.  
  207.  
  208. PROC clock()               -> erase old display, if necessary
  209.                            -> and redraw with new values
  210.  
  211. DEF xoff,yoff              -> x and y offsets from center
  212.  
  213.   /*
  214.     erase changed hands, if necessary
  215.   */
  216.   IF second<>oldsecond
  217.     xoff:=slenx!*Fcos(srad)!
  218.     yoff:=sleny!*Fsin(srad)!
  219.     Line(midx,midy,midx+xoff,midy+yoff,0)
  220.   ENDIF
  221.   IF minute<>oldminute
  222.     xoff:=mlenx!*Fcos(mrad)!
  223.     yoff:=mleny!*Fsin(mrad)!
  224.     Line(midx,midy,midx+xoff,midy+yoff,0)
  225.   ENDIF
  226.   IF hour<>oldhour OR minute<>oldminute
  227.     xoff:=hlenx!*Fcos(hrad)!
  228.     yoff:=hleny!*Fsin(hrad)!
  229.     Line(midx,midy,midx+xoff,midy+yoff,0)
  230.   ENDIF
  231.  
  232.   /*
  233.     convert to radians (minus 1.57... to normalize)
  234.   */
  235.   srad:=second!*pi_div_30-1.57075
  236.   hrad:=hour!+(minute!/60.0)*pi_div_6-1.57075
  237.   mrad:=minute!*pi_div_30-1.57075
  238.  
  239.   /*
  240.     redraw hands
  241.   */
  242.   xoff:=slenx!*Fcos(srad)!
  243.   yoff:=sleny!*Fsin(srad)!
  244.   Line(midx,midy,midx+xoff,midy+yoff,3)    -> second hand
  245.   xoff:=mlenx!*Fcos(mrad)!
  246.   yoff:=mleny!*Fsin(mrad)!
  247.   Line(midx,midy,midx+xoff,midy+yoff,2)    -> minute hand
  248.   xoff:=hlenx!*Fcos(hrad)!
  249.   yoff:=hleny!*Fsin(hrad)!
  250.   Line(midx,midy,midx+xoff,midy+yoff,1)    -> hour hand
  251. ENDPROC
  252.  
  253.  
  254. PROC dialplate()           -> clear window and draw dialpate
  255.  
  256. DEF xoff,yoff,xoff2,yoff2, -> x and y offsets from center
  257.     marks,                 -> counter variable
  258.     angle                  -> angle in radians
  259.  
  260.   /*
  261.     erase window contents
  262.   */
  263.   SetAPen(win.rport,0)
  264.   RectFill(win.rport,win.borderleft,
  265.                      win.bordertop,
  266.                      win.width-win.borderright-1,
  267.                      win.height-win.borderbottom-1)
  268.  
  269.   /*
  270.     draw hour marks as dialplate
  271.   */
  272.   FOR marks:=1 TO 12
  273.     angle:=marks!*pi_div_6-1.57075
  274.     xoff:=(radx)!*0.95*Fcos(angle)!
  275.     yoff:=(rady)!*0.95*Fsin(angle)!
  276.     xoff2:=(radx)!*Fcos(angle)!
  277.     yoff2:=(rady)!*Fsin(angle)!
  278.     Line(midx+xoff,midy+yoff,midx+xoff2,midy+yoff2,1)
  279.   ENDFOR
  280.   get_time()
  281.   clock()
  282. ENDPROC
  283.